package com.zwz.rabbitmq.producer.service;

import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.UUID;

/**
 * @Description:
 * @Auther: zwz
 * @Date: 2021-04-09-2:52 下午
 */
@Slf4j
@Service
public class OrderService {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    public void makeOrder(String userId, String productId, int num) {
        //1. 根据商品id查询库存是否充足
        //2. 保存订单
        String orderId = UUID.randomUUID().toString();

        //3. 通过MQ来完成消息的分发
        // 参数1: 交换机, 参数2: 路由key/queue队列名称   参数3: 消息内容
        String exchangeName = "fanout_order_exchange";
        String routingKey = "";
        rabbitTemplate.convertAndSend(exchangeName, routingKey,orderId);
    }

}
